//游戏主体
function GameEngine() {
	if(!GameEngine.instance) {
		GameEngine.instance = {
			score:document.getElementById("s"),
			count:0,
			level: 0,
			enemes:new Set(),
			body: document.getElementsByClassName("main")[0],
			start: function() { //开始游戏
				this.initMenu();
				var speed = 0;
				setInterval(function() {
					this.body.style.background = `url(images/bg.jpg) repeat-y 0px ${speed+=5}px`
				}.bind(this), 30)
			},
			loading: function() { //动画加载
				var logo = create("div");
				var loading = create("div");
				logo.className = "logo";
				loading.className = "loading";
				this.body.appendChild(logo);
				this.body.appendChild(loading);
				var count = 1;
				var num = 1;
				timer = setInterval(function() {
					loading.style.background = `url(images/loading${count++}.png) no-repeat`
					if(count == 4) {
						count = 1;
					}
					num++;
					if(num == 9) {
						clearInterval(timer);
						logo.remove();
						loading.remove();
						new MyPlane().init();
						this.createEnemy();
					}
				}.bind(this), 500)
			},
			initMenu: function() { //初始化菜单
				var u = document.getElementsByClassName("options")[0];
				u.addEventListener("click", function(e) {
					var e = e || event;
					var target = e.target || e.srcElement;
					if(target.nodeName.toLowerCase() == "li") {
						new GameEngine().leval = target.getAttribute("leval");
						u.remove();
						new GameEngine().loading();
					}
				}, true)
			},
			append: function(ele) {
				this.body.appendChild(ele.body)
			},
			width: function() {
				return this.body.offsetWidth;
			},
			height: function() {
				return this.body.offsetHeight;
			},
			left: function() {
				return this.body.offsetLeft;
			},
			createEnemy: function() {
				setInterval(function() {
					if(Math.random() > 0.2) {
						this.enemes.add(new EnemyPlane("small").init());
					}
				}.bind(this), 1000)
				setInterval(function() {
					if(Math.random() > 0.35) {
						this.enemes.add(new EnemyPlane("middle").init());
					}
				}.bind(this), 3000)
				setInterval(function() {
					if(Math.random() > 0.6) {
						this.enemes.add(new EnemyPlane("large").init());
					}
				}.bind(this), 8000)
				
			},
			gameover:function(){
				var div=create("div");
				div.className="gameover";
				this.body.appendChild(div);
				startMove(div,{opacity:70})
			}
		}
	}
	return GameEngine.instance;
}